#include "thrRecServer.h"

/*
    新线程: 处理服务器发来的消息
*/
void *thr_RecServerMsg(void *arg)
{
    // 接受/处理服务器消息 - 非阻塞
    msgdata msg;
    int n;

    while(1)
    {
        // 接受服务器发来的消息
        n = recv(_sockfd, &msg, sizeof(msgdata),MSG_DONTWAIT);
        if(n == -1) ;
        else if (n == 0)
        {
            printf("# the other side has been closed.\n");
            exit(0);
        }
        else
        {
//            printf("\nreceive msg:\n");
//            printMsg(&msg);

            // 根据消息类型处理消息
            switch(msg._act)
            {
                // 注册
                case _Logon: {
                    switch(msg._cancalstatue)
                    {
                        case  0:
                            printf("# Logon: Successfully!\n");
                            printf("# please login!\n");
                            login(true);
                            break;
                        case -1:
                            printf("# Logon: Acc format error!\n");
                            login(false);
                            break;
                        case -2:
                            printf("# Logon: pwd format error!\n");
                            login(false);
                            break;
                        case -3:
                            printf("# Logon: Acc cover error!\n");
                            login(false);
                            break;
                        default:    ;
                    }
                };
                break;

                // 登陆
                case _Login: {
                    switch(msg._cancalstatue)
                    {
                        case  0:
                            _isLoginFlag = true;
                            strcpy(_myacc,msg._acc);
                            printf("# Login: Successfully!\n");
                            break;
                        case -1:
                            printf("# Login: Acc error!\n");
                            login(true);
                            break;
                        case -2:
                            printf("# Login: pwd error!\n");
                            login(true);
                            break;
                        default:    ;
                    }
                };
                break;

                // 与某人聊天
                case _ChatOne: {
                    if(strcmp(msg._acc,_myacc) == 0)     // 自己发的消息发送失败
                    {
                        if(msg._cancalstatue == -1)
                            printf("# %s no online!\n",msg._who);
                    }
                    else                                 // 别人发的消息
                    {
                        printf("\n# %s: %s\n",msg._acc,msg._msg);
                        saveChatRecord(_myacc,msg._acc,msg._msg);// 保存聊天记录
                    }
                };
                break;

                // 群聊
                case _ChatGroup: {
                    printf("\n# All: %s\n",msg._msg);
                };
                break;

                case _JoinGroup: ; break;
                case _ExitGroup: ; break;
                default: printf("# Server Action Error\n"); break;
            }
        }
        usleep(100);
    }
}
